using System;
using System.Xml;
using System.IO;
using System.Text;

namespace ELS.Configuration
{
	#region ConfigurationProperty
	/// <summary>
	/// Represents a property within a <see cref="ELS.Configuration.ConfigurationSection"/>
	/// </summary>
	public class ConfigurationProperty : ConfigurationObjectBase
	{
		#region variables
		private string _name	= null;
		private ConfigurationValueCollection _values = null;
		#endregion variables

		#region constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="ELS.Configuration.ConfigurationProperty"/> class.
		/// </summary>
		/// <param name="name">The name of the property. Cannot be null or empty string("").</param>
		/// <param name="value">The value of the property. Cannot be null.</param>
		public ConfigurationProperty(string name, string value):
			this(name, new ConfigurationValueCollection(new string[]{value}))
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ELS.Configuration.ConfigurationProperty"/> class.
		/// </summary>
		/// <param name="name">The name of the property. Cannot be null or empty string("").</param>
		public ConfigurationProperty(string name ):
			this(name, new ConfigurationValueCollection(new string[0]{}))
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ELS.Configuration.ConfigurationProperty"/> class.
		/// </summary>
		/// <param name="name">The name of the property. Cannot be null or empty string("").</param>
		/// <param name="values">The values for the property.</param>
		public ConfigurationProperty(string name, ConfigurationValueCollection values)
		{
			if ( name == null || name.Length == 0)
				throw new ArgumentNullException ( "name" , "name cannot be empty or null.");

			if ( values == null )
				throw new ArgumentNullException ( "values" );

			_name	= name;
			_values	= values;
		}
		#endregion constructors

		#region public properties

		/// <summary>
		/// Gets the name of the property.
		/// </summary>
		public override string Name
		{
			get { return _name; }
		}

		/// <summary>
		/// Gets or sets the values for the property.
		/// </summary>
		public ConfigurationValueCollection Values
		{
			get { return _values;  }
			set { _values = value; }
		}

		/// <summary>
		/// Gets or sets the first value from the <see cref="ELS.Configuration.ConfigurationValueCollection"/> of the property.
		/// </summary>
		/// <remarks> Returns a null of value not found.</remarks>
		public string Value
		{
			get { return ( _values.Count == 0 ) ? null : _values [0];  }
			set 
			{
				if ( _values.Count > 0 )
					_values[0] = value; 
				else
					_values.Add( value );
			}
		}
		#endregion public properties

		#region public functions
		/// <summary>
		/// Returns the XML representation of the data stored in the <see cref="ELS.Configuration.ConfigurationProperty"/>.
		/// </summary>
		/// <returns>A string that is a representation of the data stored in the <see cref="ELS.Configuration.ConfigurationProperty"/>.</returns>
		public string GetXml()
		{
			StringBuilder sb = new StringBuilder();
			XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb));
			writer.WriteStartElement("property");
			writer.WriteAttributeString("name", Name);
			foreach(string s in Values)
			{
				writer.WriteStartElement("value");
				writer.WriteCData(s);
				writer.WriteEndElement();
			}
			writer.WriteEndElement();
			return sb.ToString();
		}
		#endregion public functions

	}
	#endregion ConfigurationProperty
}
